Skip to content

Commit 372abcf

Browse files
committed
make article & question pageview crons more robust
1 parent 904f18d commit 372abcf

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

kitsune/sumo/googleanalytics.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,16 @@ def pageviews_by_document(period, verbose=False):
288288

289289
for row in run_report(date_range, create_article_report_request, verbose=verbose):
290290
path = row.dimension_values[0].value
291-
num_page_views = int(row.metric_values[0].value)
292-
# The path is guaranteed to be a KB article path without any query parameters.
293-
# If the URL path for KB articles changes, we'll need to continue to support
294-
# the previous URL structure for a year -- the longest period of time we look
295-
# backwards -- as well as the new URL structure.
291+
# The path should be a KB article path without any query parameters, but in reality
292+
# we've seen that it can sometimes be "/". If the URL path for KB articles changes,
293+
# we'll need to continue to support the previous URL structure for a year -- the
294+
# longest period of time we look backwards -- as well as the new URL structure.
296295
# Current URL structure: /{locale}/kb/{slug}
297-
locale, slug = path.strip("/").split("/kb/")
296+
try:
297+
num_page_views = int(row.metric_values[0].value)
298+
locale, slug = path.strip("/").split("/kb/")
299+
except ValueError:
300+
continue
298301
yield ((locale, slug), num_page_views)
299302

300303

@@ -307,14 +310,18 @@ def pageviews_by_question(period=LAST_YEAR, verbose=False):
307310

308311
for row in run_report(date_range, create_question_report_request, verbose=verbose):
309312
path = row.dimension_values[0].value
310-
num_page_views = int(row.metric_values[0].value)
311-
# The path is guaranteed to be a question path without any query parameters.
312-
# If the URL path for questions changes, we'll need to continue to support
313-
# the previous URL structure for a year -- the longest period of time we look
314-
# backwards -- as well as the new URL structure.
313+
# The path should be a question path without any query parameters, but in reality
314+
# we've seen that it can sometimes be "/". If the URL path for questions changes,
315+
# we'll need to continue to support the previous URL structure for a year -- the
316+
# longest period of time we look backwards -- as well as the new URL structure.
315317
# Current URL structure: /{locale}/questions/{question_id}
316-
locale, question_id = path.strip("/").split("/questions/")
317-
yield (int(question_id), num_page_views)
318+
try:
319+
num_page_views = int(row.metric_values[0].value)
320+
locale, question_id = path.strip("/").split("/questions/")
321+
question_id = int(question_id)
322+
except ValueError:
323+
continue
324+
yield (question_id, num_page_views)
318325

319326

320327
def search_clicks_and_impressions(start_date, end_date, verbose=False):

kitsune/sumo/tests/test_googleanalytics.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ def test_pageviews_by_document(self, run_report):
7272
dimension_values=[DimensionValue(value="/en-US/kb/doc1-slug")],
7373
metric_values=[MetricValue(value="1000")],
7474
),
75+
Row(
76+
dimension_values=[DimensionValue(value="/")],
77+
metric_values=[MetricValue(value="7")],
78+
),
7579
Row(
7680
dimension_values=[DimensionValue(value="/es/kb/doc2-slug")],
7781
metric_values=[MetricValue(value="2000")],
@@ -97,6 +101,10 @@ def test_pageviews_by_question(self, run_report):
97101
dimension_values=[DimensionValue(value="/en-US/questions/123456")],
98102
metric_values=[MetricValue(value="1000")],
99103
),
104+
Row(
105+
dimension_values=[DimensionValue(value="/")],
106+
metric_values=[MetricValue(value="7")],
107+
),
100108
Row(
101109
dimension_values=[DimensionValue(value="/es/questions/782348")],
102110
metric_values=[MetricValue(value="2000")],

0 commit comments

Comments
 (0)